package com.colbby.datahouse.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ObjectLock<T> implements Lock {

	private Lock lock = new ReentrantLock();
	private T t = null;
	
	public ObjectLock(T t) {
		if(t == null) throw new RuntimeException("object cannot be null");
		this.t = t;
	}
	
	@Override
	public void lock() {
		lock.lock();
	}

	@Override
	public void lockInterruptibly() throws InterruptedException {
		lock.lockInterruptibly();
	}

	@Override
	public boolean tryLock() {
		return lock.tryLock();
	}

	@Override
	public boolean tryLock(long time, TimeUnit unit)
			throws InterruptedException {
		return lock.tryLock(time, unit);
	}

	@Override
	public void unlock() {
		lock.unlock();
	}

	@Override
	public Condition newCondition() {
		return lock.newCondition();
	}
	
	public int hashCode() {
		return t.hashCode();
	}
	
	public boolean equals(Object o) {
		if(o == null) return false;
		if(o.getClass() != this.getClass()) return false;
		ObjectLock lock = (ObjectLock) o;
		return this.t.equals(lock.t);
	}
}
